//
//  AFHomeTableView.m
//  TestPrj
//
//  Created by 朱铅峰 on 2020/5/6.
//  Copyright © 2020 Xing. All rights reserved.
//

#import "APHomeTableView.h"
#import "APHomeHeaderView.h"
#import <MJRefresh.h>

@interface APHomeTableView()<UITableViewDelegate,UITableViewDataSource>

proStrong APHomeHeaderView *headerContentView;
proStrong UIView *headerView;

@end

@implementation APHomeTableView

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    
    if (self = [super initWithFrame:frame style:style]) {
        self.dataSource = self;
        self.delegate = self;
        self.tableHeaderView = self.headerView;
        [self registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    
    return self;
}

#pragma mark - UITableViewDataSource / UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.rowCount;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"这是第%li行", indexPath.row + 1];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 44;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat contentOffsetY = scrollView.contentOffset.y;
    if (self.contentOffsetAction) {
        self.contentOffsetAction(contentOffsetY);
    }
    
    if (contentOffsetY <= 0) {
        // 当偏移量小于0时，头部视图的Y值跟随偏移量上移
        self.headerContentView.y = contentOffsetY;
    } else {
        // 头部视图滚动差的效果
        self.headerContentView.y = contentOffsetY/2;
    }
    
    self.headerContentView.contentOffsetY = contentOffsetY;
    
    // 当contentOffsetY大于零时，打开裁剪功能。而小于零时，关闭裁剪
    // 大家如果想知道不这样做的结果，就试着注释一下下面的代码试一试😜。
    //self.headerView.layer.masksToBounds = contentOffsetY > 0;
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    
    CGFloat contentOffsetY = scrollView.contentOffset.y;
    
    CGFloat kHeaderHeight = [APHomeHeaderView viewHeight];
    
    if (contentOffsetY < -kHeaderHeight / 2) {
        // 当结束滑动的偏移量小于-kHeaderHeight / 2，就开始刷新tableView
        [self.mj_header beginRefreshing];
    } else if (contentOffsetY > 0 && contentOffsetY < kHeaderHeight / 2) {
        // 当偏移量大于0并且小于kHeaderHeight / 2，就把偏移量设置在CGPointMake(0, 0)
        [self setContentOffset:CGPointMake(0, 0) animated:YES];
    } else if (contentOffsetY > kHeaderHeight / 2 && contentOffsetY < kHeaderHeight) {
        // 当偏移量大于kHeaderHeight / 2并且小于kHeaderHeight，就把偏移量设置在CGPointMake(0, kHeaderHeight)
        [self setContentOffset:CGPointMake(0, kHeaderHeight) animated:YES];
    }
}

#pragma mark - getter and setter

- (void)setRowCount:(NSInteger)rowCount {
    
    _rowCount = rowCount;
    
    [self reloadData];
}

- (UIView *)headerView {
    
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, [APHomeHeaderView viewHeight])];
        [_headerView addSubview:self.headerContentView];
    }
    return _headerView;
}

- (APHomeHeaderView *)headerContentView {
    
    if (!_headerContentView) {
        _headerContentView = [[APHomeHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.width, [APHomeHeaderView viewHeight])];
    }
    return _headerContentView;
}

@end
